package dancrawler.client;

import dancrawler.server.Gameserver;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 */
public class StartWindow extends JFrame implements ActionListener, WindowListener {

    private JButton bDirect = new JButton("Direct connection");
    private JButton bStartServer = new JButton("Start local server");
    private Gameserver gs = null;
    private Client client;
    private JTextField pname = new JTextField(20);

    public StartWindow(Client client) {
        super();
        setLocationRelativeTo(null);
        setSize(200, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        BoxLayout layout = new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS);

        this.setLayout(layout);
        this.add(new JLabel("Name:"));
        JPanel p = new JPanel();
        this.add(p);
        p.add(pname);

        this.add(bDirect);
        bDirect.addActionListener(this);
        bDirect.setAlignmentX(Component.CENTER_ALIGNMENT);
        this.add(bStartServer);
        bStartServer.addActionListener(this);
        bStartServer.setAlignmentX(Component.CENTER_ALIGNMENT);
        pack();
    }

    private void connect(InetSocketAddress addr) throws IOException {
        if(pname.getText().isEmpty())
        {
            return;
        }
        client = new Client(pname.getText());
        MainWindow mw = new MainWindow(client);
        client.addGameListener(mw);
        client.connect(addr.getHostString(), addr.getPort());
        mw.addWindowListener(this);
        mw.setVisible(true);
        this.setVisible(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == bDirect) {
            InetSocketAddress addr = null;
        try {
          addr = ConnectDialog.getAddress(this);
        } catch (UnknownHostException ex) {
          Logger.getLogger(StartWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
            if (addr != null) {
                try {
                    connect(addr);
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(this, "Could not connect!");
                }


            }
        } else if (e.getSource() == bStartServer) {
            try {
                gs = new Gameserver();
                gs.start();
                connect(new InetSocketAddress("localhost", 1881));
            } catch (IOException ex) {
            }
        }
    }

    @Override
    public void windowOpened(WindowEvent e) {
    }

    @Override
    public void windowClosing(WindowEvent e) {
    }

    @Override
    public void windowClosed(WindowEvent e) {
        this.setVisible(true);
        if (client != null) {
            client.stopClient();
        }
        if (gs != null) {
            gs.stop();
            gs = null;
        }

    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    }
}
